﻿function isInteger(str) {
    return IsValid(str, /^\d+$/);
}
function isNum(str) {
    //return IsValid(str, /^\d+(\.\d+)?$/);
    return IsValid(str, /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/);
}
function IsNumber(str) {
    return str.match(/^[0-9]*$/) != null;
}
function IsTelephone(str) {
    return IsValid(str, /^1[358]\d{9}|(0{1,2}\d{2,3}-?)?\d{7}\d?(-\d+)?$/);
}

function IsEmail(str) {
    if (str.length == 0) return true;
    return IsValid(str, /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/);
}

function notEmpty(element) {
    if (element && element.tagName === "INPUT") {
        return element.value.length > 0;
    }
}

function IsValid(val, regex) {
    if (ValidatorTrim(val).length == 0)
        return true;
    var rx = new RegExp(regex);
    var matches = rx.exec(val);
    return (matches != null && val == matches[0]);
}

function ValidatorTrim(s) {
    var m = s.match(/^\s*(\S+(\s+\S+)*)\s*$/);
    return (m == null) ? "" : m[1];
}

String.prototype.Trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

function validateCNDate(strValue) {
    strValue = strValue.Trim();
    if (strValue == "") return true;
    var objRegExp = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/

    if (!objRegExp.test(strValue)) {
        //alert(objRegExp.test(strValue));
        return false;
    }
    else {
        var arrayDate = strValue.split(RegExp.$1);
        var intYear = parseInt(arrayDate[0], 10);
        var intMonth = parseInt(arrayDate[1], 10);
        var intDay = parseInt(arrayDate[2], 10);

        if (intMonth > 12 || intMonth < 1) {
            return false;
        }

        var arrayLookup = { '1': 31, '3': 31, '4': 30, '5': 31, '6': 30, '7': 31,
            '8': 31, '9': 30, '10': 31, '11': 30, '12': 31
        }

        if (arrayLookup[intMonth] != null) {
            if (intDay <= arrayLookup[intMonth] && intDay != 0)
                return true;
        }

        if (intMonth - 2 == 0) {
            var booLeapYear = (intYear % 4 == 0 && (intYear % 100 != 0 || intYear % 400 == 0));
            if (((booLeapYear && intDay <= 29) || (!booLeapYear && intDay <= 28)) && intDay != 0)
                return true;
        }
    }
    return false;
}

function isDate(element, str, clear) {
    var retval = true;
    if (typeof (str) !== "undefined" && str !== null) {
        str = str.Trim();
        element.value = str;
    }

    if (element.value.length > 0 && !validateCNDate(element.value)) {
        if (clear && clear == true) {
            element.value = "";
        }
        retval = false;
    }
    return retval;
}